#pragma once

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <unordered_map>
#include <mutex>
#include <boost/algorithm/string.hpp>
#include "inc/Jieba.hpp"

// 计算权重的变量
#define X 10
#define Y 1
namespace ns_index
{
    struct DocInfo
    {
        std::string title;
        std::string contents;
        std::string url;
        uint64_t doc_id;
    };

    struct InverterElem
    {
        uint64_t doc_id;
        std::string word;
        int weight;
    };
    // 倒排拉链
    typedef std::vector<InverterElem> InvertedList;
	    
 
    
    class Index
    {
    private:
        std::vector<DocInfo> forward_index; // 正派索引
        // 倒排索引是一个关键字和一个倒排拉链的对应关系
        std::unordered_map<std::string, InvertedList> inverted_index;

    private:
        Index() {}
        Index(const Index& a) = delete;
        Index &operator=(const Index &a) = delete;
        static Index *instance;
        static std::mutex mtx;

    public:
        ~Index() {}

    public:
        static Index *GetInstance()
        {
            if (nullptr == instance)
            {
                mtx.lock();

                instance = new Index();

                mtx.unlock();
            }
            return instance;
        }
        DocInfo *getfile(uint64_t doc_id)
        {
            if (doc_id >= forward_index.size())
            {
                std::cerr << "doc_id error" << std::endl;
                return nullptr;
            }
            return &forward_index[doc_id];
        }
        // 用word找倒排拉链
        InvertedList *getInvertedList(const std::string &word)
        {
            auto iter = inverted_index.find(word);
            if (iter == inverted_index.end())
            {
                std::cerr << word << "have no inverlist";
                return nullptr;
            }
            return &(iter->second);
        }
        // 构建倒排和倒排索引
        bool BuildIndex(const std::string &path)
        {
            std::ifstream in(path, std::ios::in | std::ios::binary);
            if (!in.is_open())
            {
                std::cerr << "open" << path << "error" << std::endl;
                return false;
            }
            std::string line;
		int c=0;
            while (std::getline(in, line))
            {
                DocInfo *doc = BuildforwardIndex(line);
                if (nullptr == doc)
                {
                    std::cerr << "build" << line << "error" << std::endl;
                    continue;
                }
		
                BuildinverteddIndex(*doc);
		c++;
		
		std::cout<<"have build"<<c<<std::endl;
            }

            return true;
        }

    private:
        DocInfo *BuildforwardIndex(const std::string &line)
        {
            // 字符串切分
            std::vector<std::string> resultes;
            Cutstring(line, &resultes, "\3");
            if (resultes.size() != 3)
            {
                return nullptr;
            }
            DocInfo doc;
            doc.title = resultes[0];
            doc.contents = resultes[2];
            doc.url = resultes[1];
            doc.doc_id = forward_index.size();
            forward_index.push_back(std::move(doc));
            return &forward_index.back();
        }
        bool BuildinverteddIndex(const DocInfo &doc)
        {
            struct word_cnt
            {
                int title_cnt;
                int content_cnt;
                word_cnt() : title_cnt(0), content_cnt(0) {}
            };
		cppjieba::Jieba jieba("./dict/jieba.dict.utf8",
"./dict/hmm_model.utf8",
"./dict/user.dict.utf8",
"./dict/idf.utf8",
"./dict/stop_words.utf8");
            std::unordered_map<std::string, word_cnt> word_map;
            // 词频统计，标题/内容
            std::vector<std::string> title_words;
            
		jieba.Cut(doc.title, title_words, true);
            for (std::string &s : title_words)
            {
                // 转小写
                boost::to_lower(s);
                word_map[s].title_cnt++; // 不存在会新建
            }
            std::vector<std::string> content_words;
		jieba.Cut(doc.contents, content_words, true);
            for (std::string &s : content_words)
            {
                boost::to_lower(s);
                word_map[s].content_cnt++;
            }

            for (auto &word_pair : word_map)
            {
                InverterElem item;
                item.doc_id = doc.doc_id;
                item.word= word_pair.first;
                item.weight = Y * word_pair.second.content_cnt + X * word_pair.second.title_cnt;
                inverted_index[word_pair.first].push_back(std::move(item));
            }

            return true;
        }
        void Cutstring(const std::string &line, std::vector<std::string> *out, const std::string sep)
        {
            boost::split(*out, line, boost::is_any_of(sep), boost::token_compress_on);
        }
    };
Index *Index::instance = nullptr;
std::mutex Index::mtx;

};
